/*
 * Copyright (C) 2013 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** http://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-7 */
export class ErrorCode{
  httpCode: number
  constructor(httpCode: number) {
    this.httpCode = httpCode
  }
  /** Not an error!  */
  static NO_ERROR = new ErrorCode(0)

  static PROTOCOL_ERROR = new ErrorCode(1)

  static INTERNAL_ERROR = new ErrorCode(2)

  static FLOW_CONTROL_ERROR = new ErrorCode(3)

  static SETTINGS_TIMEOUT = new ErrorCode(4)

  static STREAM_CLOSED = new ErrorCode(5)

  static FRAME_SIZE_ERROR = new ErrorCode(6)

  static REFUSED_STREAM = new ErrorCode(7)

  static CANCEL = new ErrorCode(8)

  static COMPRESSION_ERROR = new ErrorCode(9)

  static CONNECT_ERROR = new ErrorCode(0xa)

  static ENHANCE_YOUR_CALM = new ErrorCode(0xb)

  static INADEQUATE_SECURITY = new ErrorCode(0xc)

  static HTTP_1_1_REQUIRED = new ErrorCode(0xd)

  fromHttp2(code: number): ErrorCode | null {
    switch (code){
      case 0:
        return ErrorCode.NO_ERROR
      case 1:
        return ErrorCode.PROTOCOL_ERROR
      case 2:
        return ErrorCode.INTERNAL_ERROR
      case 3:
        return ErrorCode.FLOW_CONTROL_ERROR
      case 4:
        return ErrorCode.SETTINGS_TIMEOUT
      case 5:
        return ErrorCode.STREAM_CLOSED
      case 6:
        return ErrorCode.FRAME_SIZE_ERROR
      case 7:
        return ErrorCode.REFUSED_STREAM
      case 8:
        return ErrorCode.CANCEL
      case 9:
        return ErrorCode.COMPRESSION_ERROR
      case 0xa:
        return ErrorCode.CONNECT_ERROR
      case 0xb:
        return ErrorCode.ENHANCE_YOUR_CALM
      case 0xc:
        return ErrorCode.INADEQUATE_SECURITY
      case 0xd:
        return ErrorCode.HTTP_1_1_REQUIRED
    }
  }
}
